<template>
  <div>
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item :label="$t('m.system.datasource.entity.datasourceKey')">
              <a-input placeholder="" v-model="queryParam.datasourceKey"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item :label="$t('m.system.datasource.entity.datasourceName')">
              <a-input placeholder="" v-model="queryParam.datasourceName"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :md="6" :sm="8">
              <a-form-item :label="$t('m.system.datasource.entity.datasourceType')">
                <a-input placeholder="" v-model="queryParam.datasourceType"></a-input>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{ $t('m.common.button.query') }}</a-button>
              <a-button type="default" @click="searchReset" icon="reload" style="margin-left: 8px">{{ $t('m.common.button.reset') }}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? this.$t('m.common.button.fold') : this.$t('m.common.button.expand') }}
                <ych-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div v-if="!isSelectModal" class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">{{ $t('m.common.button.add') }}</a-button>
<!--      <a-button @click="batchDel" type="default"  icon="delete">{{ $t('m.common.button.delete') }}</a-button>-->
      <!--<a-button type="default" icon="download" @click="handleExportXls('数据源配置')">{{ $t('m.common.button.export') }}</a-button>-->
      <!--<a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"-->
                <!--@change="handleImportExcel">-->
        <!--<a-button type="default" icon="import">{{ $t('m.common.button.import') }}</a-button>-->
      <!--</a-upload>-->
    </div>

    <!-- table区域-begin -->
    <div>

      <a-table
        ref="table"
        size="middle"
        bordered
        :rowKey="rowKey"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{fixed:true,selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: (radio ? 'radio' : 'checkbox')}"
        @change="handleTableChange"
        :customRow="clickThenCheck"
      >
        <template slot="serialNum" slot-scope="text,record,index">
          {{(ipagination.current-1) * ipagination.pageSize + parseInt(index) + 1}}
        </template>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无此图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="图片不存在"
               style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无此文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="uploadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)" v-if="record.datasourceKey !== 'dataSourceDefault'">{{ $t('m.common.button.update') }}</a>
          <a-divider type="vertical" v-if="record.datasourceKey !== 'dataSourceDefault'"/>
          <a-popconfirm :title="$t('m.common.tip.delete_confirm')" @confirm="() => handleDelete(record.id)" v-if="record.datasourceKey !== 'dataSourceDefault'">
            <a>{{ $t('m.common.button.delete') }}</a>
          </a-popconfirm>
        </span>

      </a-table>
    </div>

    <sysDatasourceConfig-modal ref="modalForm" @ok="modalFormOk"></sysDatasourceConfig-modal>
    </div>
</template>

<script>

  import { YcListMixin } from '@/mixins/YcListMixin'
  import SysDatasourceConfigModal from './modules/SysDatasourceConfigModal'
  import YchConstant from "@/api/YchConstant";

  export default {
    name: 'SysDatasourceConfigList',
    mixins: [YcListMixin],
    components: {
      SysDatasourceConfigModal
    },
    props: {
      isSelectModal: {
        type: Boolean,
        default: false
      },
      radio: {
        type: Boolean,
        default: false
      }
    },
    data () {
      return {
        description: '数据源配置管理页面',
        // 表头
        columns: [
          {
            title:this.$t('m.common.label.number'),
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: 'center',
            scopedSlots: { customRender: 'serialNum' },
          },
          {
            title: this.$t('m.system.datasource.entity.datasourceKey'),
            align: 'center',
            dataIndex: 'datasourceKey'
          },
          {
            title: this.$t('m.system.datasource.entity.datasourceName'),
            align: 'center',
            dataIndex: 'datasourceName'
          },
          {
            title: this.$t('m.system.datasource.entity.datasourceType'),
            align: 'center',
            dataIndex: 'datasourceType'
          },
          {
            title: this.$t('m.system.datasource.entity.dbType'),
            align: 'center',
            dataIndex: 'dbType'
          },
          {
            title: this.$t('m.common.label.operate'),
            dataIndex: 'action',
            align: 'center',
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: YchConstant.PREFIX_SYSTEM + '/sysDatasourceConfig/list',
          delete: YchConstant.PREFIX_SYSTEM + '/sysDatasourceConfig/delete',
          deleteBatch: YchConstant.PREFIX_SYSTEM + '/sysDatasourceConfig/deleteBatch',
          exportXlsUrl: YchConstant.PREFIX_SYSTEM + '/sysDatasourceConfig/exportExcel',
          importExcelUrl: YchConstant.PREFIX_SYSTEM + '/sysDatasourceConfig/importExcel',
        },
        dictOptions: {},
      }
    },
    computed: {
      importExcelUrl: function () {
        return `${bootConfig[process.env.NODE_ENV].VUE_APP_API}${this.url.importExcelUrl}`
      }
    },
    methods: {
      initDictConfig () {
        if (this.isSelectModal) {
          this.columns.splice(this.columns.length - 1)
        }
      }
    }
  }
</script>
<style lang="less" scoped>

</style>
